package com.gmrz.uap.util;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ApkFacetIDUtil {

    private static final Logger LOG = Logger.getLogger(ApkFacetIDUtil.class);

    public static String exec(String filePath) {
        Process p;
        String cmd = "openssl pkcs7 -in " + filePath + " -inform DER -print_certs | openssl x509 -outform DER | openssl sha1 -binary | openssl base64 | sed 's/=//g' | awk '{print \"android:apk-key-hash:\"$0}'";

        String result = null;
        try {

            LOG.info("APK生成语句 : " + cmd);

            //执行命令
            p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
            //取得命令结果的输出流
            InputStream fis = p.getInputStream();
            //用一个读输出流类去读
            InputStreamReader isr = new InputStreamReader(fis);
            //用缓冲器读行
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            //直到读完为止
            while ((line = br.readLine()) != null) {
                result = line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {
        String filePath = "/Users/Edgar/work/other/zip_tmp/META-INF/CERT.RSA";
        System.out.println(exec(filePath));
    }

}
